//app.js
App({
  onLaunch: function () {
    var that = this;
  },
  
  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登陆接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },

  /**
   * 登录 
   */
  login: function (page, type) {
    console.log('login')
    let that = this
    wx.login({
      success: function (res) {
        let code = res.code
        if (code) {
          wx.getSetting({
            success: function (res) {
              console.log(res.authSetting)

              if (res.authSetting['scope.userInfo']) {
                console.log('正确授权')
                wx.getUserInfo({
                  success: function (res) {
                    let rowData = res.rawData
                    let signature = res.signature
                    let encryptedData = res.encryptedData
                    let iv = res.iv
                    that.globalData.userInfo = res.userInfo
                    wx.request({
                      url: that.apiUrl('/public/wxaLogin'),
                      method: 'POST',
                      data: {
                        code: code,
                        rawData: rowData,
                        signature: signature,
                        encryptedData: encryptedData,
                        iv: iv
                      },
                      success: function (res) {
                        console.log('wxaLogin')
                        console.log(res.data)
                        //console.log(res.header)
                        if (res.data.data) {
                          let token = res.data.data.token
                          //console.log(res.data.data)
                          try {
                            if (token) {
                              wx.setStorageSync('token', token)
                              that.globalData.token = token
                              if (type == 0)
                                page.onLoad()
                              else 
                                page.onShow()
                            }

                          } catch (e) {
                            console.log("storage error:" + e)
                          }
                        }

                      },
                      fail: function (res) {
                        wx.showToast({
                          title: '网络请求异常',
                          icon: 'none',
                          duration: 2000
                        })

                      },
                      complete: function (res) {
                        // wx.hideToast()

                      }
                    })
                  }, fail: function (res) {
                    // wx.hideToast()
                    wx.showToast({
                      title: '获取用户信息失败',
                      icon: 'none',
                      duration: 2000
                    })
                  }
                })

              } else {
                if (!that.globalData.ifAuthorization) {
                  that.globalData.ifAuthorization = true
                  wx.redirectTo({
                    url: '/pages/wx-authorization/index',
                  })
                }
                  
              }
            }, fail: function (res) {
              wx.showToast({
                title: '网络请求异常',
                icon: 'none',
                duration: 2000
              })
            }
          })

        } else {

        }
      },
      fail: function (res) {
        // wx.hideToast()
        wx.showToast({
          title: '网络请求异常',
          icon: 'none',
          duration: 2000
        })

      }, complete: function (res) {

      }
    })
  },

  apiUrl: function (e) {
    var url = "http://c.cb.xianlaohu.com/wxa"
    return url + e;
  },

  /**
   * 分享小程序
   */
  shareMessage: function (res) {
    // console.log('lalala3')
    return {
      title: "鲜商" + '——' + this.globalData.shareProfile,
      path: this.globalData.sharePath,
      imageUrl: '/images/default-head.png',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },

  globalData:{
    userInfo: null,
    version: "1.0",
    shareProfile: '绿色生鲜食品的购买平台', 
    imgUrl: "http://c.cb.xianlaohu.com/public/uploads/",
    token: '',
    unknownError: '未知错误',
    sharePath: '/pages/index/index',
    successCode: 10000,
    toastTimeout: 15001,
    ifAuthorization: false
  },
})
